<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/25
 * Time: 11:31
 */
class sms
{
	function __construct()
	{
		$this->sms();
	}
	function sms(){
		include "TopSdk.php";
		include_once "TopConfig.php";
	}
	/*
	 * 发送短信
	 * 成功：object(stdClass)#7 (2) { ["result"]=> object(stdClass)#8 (4) { ["code"]=> int(1) ["datas"]=> object(stdClass)#9 (0) { } ["message"]=> string(7) "SUCCESS" ["successful"]=> bool(true) } ["request_id"]=> string(13) "12cklbnjkyinb" }
	 * 失败：object(stdClass)#7 (2) { ["result"]=> object(stdClass)#8 (3) { ["code"]=> int(10001) ["message"]=> string(13) "ILLEGAL_PARAM" ["successful"]=> bool(false) } ["request_id"]=> string(12) "qm37iytrddys" }
	 */
	public function send($mobile,$domain,$template_id,$templateval=null,$external_id=""){

		date_default_timezone_set('Asia/Shanghai');
		$c = new TopClient(BAICHUANAPPKEY,BAICHUANSECRETKEY);
		$c->format="json";
		$req = new OpenSmsSendmsgRequest;
		$content='{';
		if($templateval){
			foreach($templateval as $key=>$val){
				$content.='"'.$key.'":"'.$val.'",';
			}
		}
		$content=trim($content,',');
		$content.='}';
		$send_message_request = new SendMessageRequest;
		$send_message_request->template_id=$template_id;
		$send_message_request->signature_id=BAICHUANSIGNID;
		$send_message_request->external_id=$external_id;
		$send_message_request->mobile=$mobile;
		$send_message_request->device_limit="20";
		$send_message_request->session_limit="20";
		$send_message_request->device_limit_in_time="20";
		$send_message_request->mobile_limit="20";
		$send_message_request->session_limit_in_time="20";
		$send_message_request->mobile_limit_in_time="20";
		$send_message_request->session_id=$mobile;
		$send_message_request->domain=$domain;
		$send_message_request->device_id=$mobile;
		$send_message_request->delay_time="500";
		$send_message_request->context=$content;
		//        $send_message_request->signature="淘宝网";
		$req->setSendMessageRequest(json_encode($send_message_request));
		$resp = $c->execute($req);
		return $resp;
	}
	/*
	 * 发送短信验证码
	 * 返回：
	 * object(stdClass)#7 (2) { ["result"]=> object(stdClass)#8 (4) { ["code"]=> int(1) ["datas"]=> object(stdClass)#9 (2) { ["sms_size"]=> int(1) ["task_id"]=> float(4.3212525637978E+17) } ["message"]=> string(7) "SUCCESS" ["successful"]=> bool(true) } ["request_id"]=> string(12) "z27tjdbxlxvx" }
	 */
	public function sendcheck($mobile,$domain,$template_id,$templateval=null,$mobile_limit_in_time=40, $expire_time = 480,$external_id="")
	{
		date_default_timezone_set('Asia/Shanghai');
		$c = new TopClient(BAICHUANAPPKEY,BAICHUANSECRETKEY);
		$c->format="json";

		$req = new OpenSmsSendvercodeRequest;
		if($templateval){
			foreach($templateval as $key=>$val){
				$req->putOtherTextParam($key,$val);
			}
		}
		$send_ver_code_request = new SendVerCodeRequest;
		/**
		 * 验证码失效时间，单位为秒
		 **/
		$send_ver_code_request->expire_time = $expire_time;
		$send_ver_code_request->session_limit = "200";
		$send_ver_code_request->device_limit = "20";
		$send_ver_code_request->device_limit_in_time = "20";
		$send_ver_code_request->mobile_limit = "20";
		$send_ver_code_request->session_limit_in_time = "20";
		/**
		 * 外部的id，发送失败的消息通知会原封不动的带回，用于和已有的状态进行关联
		 **/
		$send_ver_code_request->external_id = $external_id;
		/**
		 * 手机号的次数限制的时间，单位为秒
		 **/
		$send_ver_code_request->mobile_limit_in_time = $expire_time;
		$send_ver_code_request->template_id = $template_id;
		$send_ver_code_request->signature_id = BAICHUANSIGNID;
		$send_ver_code_request->session_id = "ideagold";
		$send_ver_code_request->domain = $domain;
		//        $send_ver_code_request->device_id = $mobile;
		$send_ver_code_request->mobile = $mobile;
		//        $send_ver_code_request->ver_code_length = "4";
		//        $send_ver_code_request->signature = "淘宝网";
		$req->setSendVerCodeRequest(json_encode($send_ver_code_request));
		$resp = $c->execute($req);
		return $resp;
	}

	/*
	 * 验证短信
	 * 成功：object(stdClass)#7 (2) { ["result"]=> object(stdClass)#8 (3) { ["code"]=> int(1) ["message"]=> string(7) "SUCCESS" ["successful"]=> bool(true) } ["request_id"]=> string(12) "ze2789k7ncw6" }
	 * 失败：object(stdClass)#7 (2) { ["result"]=> object(stdClass)#8 (3) { ["code"]=> int(10003) ["message"]=> string(33) "验证码错误，请重新输入" ["successful"]=> bool(false) } ["request_id"]=> string(13) "10fd348upcvvf" }
	 */
	public function check($mobile,$domain,$ver_code)
	{
		date_default_timezone_set('Asia/Shanghai');
		$c = new TopClient(BAICHUANAPPKEY,BAICHUANSECRETKEY);
		$c->format="json";
		$req = new OpenSmsCheckvercodeRequest;
		$check_ver_code_request = new CheckVerCodeRequest;
		$check_ver_code_request->domain=$domain;
		$check_ver_code_request->check_fail_limit="6";
		$check_ver_code_request->check_success_limit="1";
		$check_ver_code_request->ver_code=$ver_code;
		$check_ver_code_request->mobile=$mobile;
		$req->setCheckVerCodeRequest(json_encode($check_ver_code_request));
		$resp = $c->execute($req);
		return $resp;
	}
}